import {
  request
} from "../../request/index.js";
var app = getApp();
var icon = true;
var util = require("../../utils/util.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbar: ['废钢', '二手钢材', '二手钢结构', '二手设备', '废纸塑料', '有色金属', '一手户头', '行情报价'],
    currentTab: 0,
    playIcon: "/icon/sc.png", //默认图片地址
    title: "收藏",
    scrollLeft: 0,
    resourcesList: [],
    pageNum: 1,
    total: '',
    options: 0,
    ic: [],
    hid: true
  },
  call(e) {
    var time = this.data.user.memberDueDate
    var dd = util.formatDate(new Date())
    var that = this
    var phone = e.currentTarget.dataset.ph
    var userId = app.globalData.userId;
    var userIdByResource=e.currentTarget.dataset.u;
    var resourcesId = e.currentTarget.dataset.r;
    if(this.data.user.userType==1){
      wx.showToast({
        title: '您不是会员，请充值查看',
        icon: "none",
        duration: 1500
      })
      setTimeout(function () {
        wx.navigateTo({
          url: '/pages/member/member?userId=' + userId + "&userIdByResource=" + userIdByResource,
        })
      }, 1500)
      return;
    }
    if (!time) {
      wx.showToast({
        title: '您不是会员，请充值查看',
        icon: "none",
        duration: 1500
      })
      setTimeout(function () {
        wx.navigateTo({
          url: '/pages/member/member?userId=' + userId+"&userIdByResource="+userIdByResource,
        })
      }, 1500)

      // wx.showToast({
      //   title: '',
      //   icon: "none",
      //   duration: 1500,
      //   success: function () {
      //     setTimeout(function () {
      //       wx.navigateTo({
      //         url: '',
      //       })
      //     }, 1500)
      //   }
      // })

    } else {
      if (dd > time) {
        wx.showToast({
          title: '您的会员已到期，请充值查看',
          icon: "none",
          duration: 1500
        })
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/member/member?userId=' + userId+"&userIdByResource="+userIdByResource,
          })
        }, 1500)
      } else {
        request({
          url: "/UserInfo/getUserViewPhoneLogByUserId",
          data: {
            userId: userId
          }
        }).then(res => {
          var flag=false;
          for(var i=0;i<res.data.length;i++){
            if(res.data[i].resourcesId==resourcesId){
              flag=true;
            }
          }
          if(that.data.user.userType==0){
            flag=true
          }
          if(flag==true){
            wx.showModal({
              title: "电话",
              content: phone,
              success(res) {
                if (res.confirm) {
                  wx.makePhoneCall({
                    phoneNumber: phone,
                  })
                } else if (res.cancel) {}
              }
            })
          }else if (res.data.length < 7&&flag==false) {
            request({
                  url:"/UserInfo/addUserViewPhoneLog",
                  data:{
                    userId:userId,
                    resourcesId:resourcesId,
                    phone:phone
                  }
                })
            wx.showModal({
              title: "电话",
              content: phone,
              success(res) {
                if (res.confirm) {
                  wx.makePhoneCall({
                    phoneNumber: phone,
                  })
                } else if (res.cancel) {}
              }
            })

          } else {
            wx.showToast({
              title: '您今日的查看次数已达上限',
              icon: "none",
              duration: 1500
            })
          }
        })
      }
    }
  },
  navbarTap: function (e) {
    var that = this
    if (e.currentTarget.dataset.idx == 0) {
      that.data.options = 11;
      this.getResourcesList1(that.data.options);
      this.setData({
        hid: true
      })
    } else if (e.currentTarget.dataset.idx == 1) {
      that.data.options = 12;
      this.getResourcesList1(that.data.options);
      this.setData({
        hid: true
      })
    } else if (e.currentTarget.dataset.idx == 2) {
      that.data.options = 13;
      this.getResourcesList1(that.data.options);
      this.setData({
        hid: true
      })
    } else if (e.currentTarget.dataset.idx == 3) {
      that.data.options = 14;
      this.getResourcesList1(that.data.options);
      this.setData({
        hid: true
      })
    } else if (e.currentTarget.dataset.idx == 4) {
      that.data.options = 15;
      this.getResourcesList1(that.data.options);
      this.setData({
        hid: true
      })
    } else if (e.currentTarget.dataset.idx == 5) {
      that.data.options = 16;
      this.getResourcesList1(that.data.options);
      this.setData({
        hid: true
      })
    } else if (e.currentTarget.dataset.idx == 6) {
      that.data.options = 17;
      this.getResourcesList1(that.data.options);
      this.setData({
        hid: true
      })
    } else if (e.currentTarget.dataset.idx == 7) {
      that.data.options = 2;
      this.getResourcesList1(that.data.options);
      this.setData({
        hid: false
      })
    }
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })
  },
  getResourcesList1(resourceType) {
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    request({
      url: "/Resources/getAllResources",
      data: {
        resourcesType: resourceType,
        pageNum: 1,
        resourcesStatus: 1
      }
    }).then(res => {
      var list = res.data.list
      this.setData({
        resourcesList: list,
        total: res.data.total,
        pageNum: 1
      })
    }).then(r => {
      this.setData({
        ic: []
      })
      var userId = app.globalData.userId;
      for (var i = 0; i < that.data.resourcesList.length; i++) {
        var resourcesId = that.data.resourcesList[i].resourcesId;
        request({
          url: "/Favorite/isFavorite",
          data: {
            userId: userId,
            resourcesId: resourcesId,
            resourcesStatus: 1
          }
        }).then(rr => {
          var c = that.data.ic
          c.push(rr.data)
          that.setData({
            ic: c
          })
        })
      }
    })
    wx.hideLoading()
  },
  getResourcesList(resourceType) {
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    request({
      url: "/Resources/getAllResources",
      data: {
        resourcesType: resourceType,
        pageNum: that.data.pageNum,
        resourcesStatus: 1
      }
    }).then(res => {
      var list1 = that.data.resourcesList;
      var list2 = res.data.list;
      list1 = list1.concat(list2);
      this.setData({
        resourcesList: list1,
        total: res.data.total
      })
    }).then(r => {
      this.setData({
        ic: []
      })
      var userId = app.globalData.userId;
      for (var i = 0; i < that.data.resourcesList.length; i++) {
        var resourcesId = that.data.resourcesList[i].resourcesId;
        request({
          url: "/Favorite/isFavorite",
          data: {
            userId: userId,
            resourcesId: resourcesId
          }
        }).then(rr => {
          var c = that.data.ic
          c.push(rr.data)
          that.setData({
            ic: c
          })
        })
      }
    })
    wx.hideLoading()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.isshare == 1) {
      wx.navigateTo({
        url: '../guang/guang'
      })
    }
    var that = this
    that.data.options = options.resourcesType
    var userId = app.globalData.userId;
    this.getResourcesList(that.data.options)
    this.setData({
      currentTab: parseInt(options.currentTab)
    })
    if (parseInt(options.currentTab) == 5) {
      this.setData({
        scrollLeft: 200
      })
    } else if (parseInt(options.currentTab) == 6) {
      this.setData({
        scrollLeft: 300
      })
    } else if (parseInt(options.currentTab) == 7) {
      this.setData({
        scrollLeft: 400,
        hid: false
      })
    } else if (parseInt(options.currentTab) == 4) {
      this.setData({
        scrollLeft: 100
      })
    }
    request({
      url: "/UserInfo/getUserInfo",
      data: {
        userId: userId
      }
    }).then(re => {
      this.setData({
        user: re.data
      })
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.pageNum * 10 < that.data.total) {
      var pageNum = that.data.pageNum + 1;
      that.setData({
        pageNum: pageNum
      });
      that.getResourcesList(that.data.options)
    } else {
      wx.showToast({
        title: '已经到底了',
        icon: "none",
        duration: 1000
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: '/pages/demo2/demo2?isshare=1',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})